﻿using System;
using System.Collections.Generic;
using System.Linq;
using Altairis.FluffyCloud.Data;
using Altairis.FluffyCloud.Data.Views;

namespace Altairis.FluffyCloud.WebCore.Pages.My {

    public partial class HomePage : DbPage {

        public IQueryable<TopicInfo> MyBookmarks_GetData() {
            var q = from s in this.DbContext.TopicUserStates
                    where s.UserName.Equals(this.User.Identity.Name, StringComparison.CurrentCultureIgnoreCase) && s.IsBookmarked
                    select new TopicInfo {
                        TopicId = s.TopicId,
                        LastVisitDate = s.LastVisitDate,
                        DateCreated = s.Topic.DateCreated,
                        Description = s.Topic.Description,
                        Name = s.Topic.Name,
                        UserName = s.Topic.UserName,
                        PostCount = s.Topic.Posts.Count,
                        LastPostDate = s.Topic.Posts.Any() ? s.Topic.Posts.OrderByDescending(p => p.DateCreated).FirstOrDefault().DateCreated : DateTimeOffset.MaxValue,
                        LastPostUserName = s.Topic.Posts.Any() ? s.Topic.Posts.OrderByDescending(p => p.DateCreated).FirstOrDefault().UserName : null,
                    };
            return q.OrderByDescending(x => x.LastPostDate);
        }

        public IQueryable<Altairis.FluffyCloud.Data.Views.TopicInfo> MyTopics_GetData() {
            var q = from s in this.DbContext.TopicUserStates
                    where s.UserName.Equals(this.User.Identity.Name, StringComparison.CurrentCultureIgnoreCase) && s.Topic.UserName.Equals(this.User.Identity.Name, StringComparison.CurrentCultureIgnoreCase)
                    select new TopicInfo {
                        TopicId = s.TopicId,
                        LastVisitDate = s.LastVisitDate,
                        DateCreated = s.Topic.DateCreated,
                        Description = s.Topic.Description,
                        Name = s.Topic.Name,
                        UserName = s.Topic.UserName,
                        PostCount = s.Topic.Posts.Count,
                        LastPostDate = s.Topic.Posts.Any() ? s.Topic.Posts.OrderByDescending(p => p.DateCreated).FirstOrDefault().DateCreated : DateTimeOffset.MaxValue,
                        LastPostUserName = s.Topic.Posts.Any() ? s.Topic.Posts.OrderByDescending(p => p.DateCreated).FirstOrDefault().UserName : null,
                    };
            return q.OrderByDescending(x => x.LastPostDate);
        }

        public IEnumerable<Topic> NewForumList_GetData() {
            return this.DbContext.Topics.OrderByDescending(x => x.DateCreated).Take(5);
        }

        public IEnumerable<User> NewUserList_GetData() {
            return this.DbContext.Users.Where(x => x.IsApproved).OrderByDescending(x => x.DateCreated).Take(5);
        }

        protected void Page_Load(object sender, EventArgs e) {
            int topicId;
            if (int.TryParse(this.Request.QueryString["bookmark-add"], out topicId)) {
                // Add to bookmarks
                var s = this.DbContext.TopicUserStates.SingleOrDefault(x => x.UserName.Equals(this.User.Identity.Name) && x.TopicId == topicId);
                if (s == null) {
                    s = new TopicUserState {
                        TopicId = topicId,
                        UserName = this.User.Identity.Name,
                        LastVisitDate = DateTime.UtcNow,
                        IsBookmarked = true
                    };
                    this.DbContext.TopicUserStates.Add(s);
                }
                else {
                    s.IsBookmarked = true;
                }
                this.DbContext.SaveChanges();
                this.Response.RedirectToRoute("MyHome");
            }
            else if (int.TryParse(this.Request.QueryString["bookmark-delete"], out topicId)) {
                // Delete from bookmarks
                var s = this.DbContext.TopicUserStates.SingleOrDefault(x => x.UserName.Equals(this.User.Identity.Name) && x.TopicId == topicId && x.IsBookmarked);
                if (s != null) {
                    s.IsBookmarked = false;
                    this.DbContext.SaveChanges();
                }
                this.Response.RedirectToRoute("MyHome");
            }
        }
    }
}